//
//  L0TextEntryCell.m
//  L0EntryTable
//
//  Created by ∞ on 18/07/08.
//  Copyright 2008 Emanuele Vulcano. All rights reserved.
//

#import "L0TextEntryCell.h"
#import "L0EntrySection.h"
#import "L0EntryTableController.h"

@implementation L0TextEntryCell

@synthesize textField;
- (void) setTextField:(UITextField*) field {
	if (field != textField) {
		[textField release];
		textField = [field retain];
		
		textField.delegate = self;
	}
}

@synthesize textFieldDelegate;

- (void) dealloc {
	self.textField = nil;
	[super dealloc];
}

- (BOOL) textFieldShouldBeginEditing:(UITextField*) tf {
	BOOL delegateAllows = YES;
	if ([self.textFieldDelegate respondsToSelector:@selector(textFieldShouldBeginEditing:)])
		delegateAllows = ([self.textFieldDelegate performSelector:@selector(textFieldShouldBeginEditing:) withObject:tf] != (id)0);
	
	return delegateAllows && self.section.controller.entryAllowed;
}

- (void) textFieldDidBeginEditing:(UITextField*) tf {
	[self select];
	if ([self.textFieldDelegate respondsToSelector:@selector(textFieldDidBeginEditing:)])
		[self.textFieldDelegate performSelector:@selector(textFieldDidBeginEditing:) withObject:tf];
}

- (void) setUpEditing {
	[self.textField becomeFirstResponder];
}

- (void) tearDownEditing {
	[self.textField resignFirstResponder];
}

- (BOOL) textFieldShouldReturn:(UITextField*) tf {
	BOOL delegateAllows = YES;
	if ([self.textFieldDelegate respondsToSelector:@selector(textFieldShouldReturn:)])
		delegateAllows = ([self.textFieldDelegate performSelector:@selector(textFieldShouldReturn:) withObject:tf] != (id)0);
	
	if (delegateAllows && self.textField.returnKeyType == UIReturnKeyDefault)
		[self selectNextCellDisplayingEditor];
	
	return delegateAllows;
}

- (void) forwardInvocation:(NSInvocation*) i {
	if ([self.textFieldDelegate respondsToSelector:[i selector]])
		[i invokeWithTarget:self.textFieldDelegate];
	else
		[super forwardInvocation:i];
}

- (BOOL) displaysKeyboardOnSetUp { return YES; }

@end
